/*
 * @Description: 通知按钮
 * @Author: Zzt
 * @Date: 2023-02-20 15:49:32
 * @LastEditTime: 2023-02-28 14:47:25
 */
import { cancelAnnouncement, publishAnnouncement } from '@/api/announcement/announcement';
import { Button, Divider, message, Space } from 'antd';
import classNames from 'classnames/bind';
import { useMemo, useState } from 'react';
import { useAnnouncementMgtContext } from '../contexts/AnnouncementMgtContext';
import styles from '../styles/operate.module.scss';
import AddAnnounce from './dialogs/AddAnnounce';

const cx = classNames.bind(styles);

const PageOperate = () => {
  const { selectItems, delAnnounce } = useAnnouncementMgtContext();
  const [addAnnounceVisible, setAddAnnounceVisible] = useState(false);

  const AddAnnounceList = () => {
    setAddAnnounceVisible(true);
  };

  const Release = () => {
    if (selectItems.length === 0) {
      message.warning('请选择一项进行发布');
    } else {
      if (selectItems.length === 1) {
        publishAnnouncement(selectItems[0]);
      } else {
        message.warning('请一次发布一条公告');
      }
    }
  };
  const Unpublish = () => {
    if (selectItems.length === 0) {
      message.warning('请选择一项取消发布');
    } else {
      if (selectItems.length === 1) {
        cancelAnnouncement(selectItems[0]);
      } else {
        message.warning('请一次取消一条公告');
      }
    }
  };
  const Delect = () => {
    if (selectItems.length === 0) {
      message.warning('请选择一项进行删除');
    } else {
      // TODO 调用接口
      delAnnounce(selectItems, `确认删除选中的${selectItems.length}项公告吗？`);
      console.log('删除');
    }
  };
  return (
    <>
      <div className={cx('operate')}>
        <b>发布公告</b>
        <div>
          <Space>
            <Button type="primary" onClick={AddAnnounceList}>
              新建公告
            </Button>
            <Button onClick={Release}>发布</Button>
            <Button onClick={Unpublish}>撤回</Button>
            <Button onClick={Delect}>删除</Button>
            <Divider type="vertical" />
            <Button>筛选检索</Button>
          </Space>
        </div>
      </div>
      {useMemo(
        () => (
          <AddAnnounce visible={addAnnounceVisible} onClose={() => setAddAnnounceVisible(false)} />
        ),
        [addAnnounceVisible]
      )}
    </>
  );
};
export default PageOperate;
